﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SD.LLBLGen.Pro.ORMSupportClasses;
using WHMS;
using WHMS.EntityClasses;
using WMS.HelperClasses;
using WHMS.HelperClasses;
using Microsoft.Reporting.WinForms;
using System.Reflection;

namespace WMS.Reports
{
    public partial class InventoryReport : Form
    {
        private int _selectedStoreId;
        private EntityCollection<InventoryItemEntity> _inventoryItems;
        private bool _hideQty;

        public InventoryReport()
        {
            InitializeComponent();
        }

        public InventoryReport(bool hideQty)
        {
            InitializeComponent();
            _hideQty = hideQty;
        }

        private void InventoryReport_Load(object sender, EventArgs e)
        {
            new FormsHelper().DisableReportExportToWord(this.reportViewer1);
        }

        private void txtStoreCode_TextChanged(object sender, EventArgs e)
        {
            DataTable DTStore = Business.GetStoreDetails(txtStoreCode.Text);
            if (DTStore.Rows.Count > 0)
            {
                _selectedStoreId = Int32.Parse(DTStore.Rows[0][0].ToString());
                lblStoreName.Text = DTStore.Rows[0][2].ToString();
                btnShowReport.Enabled = true;
            }
            else
            {
                lblStoreName.Text = string.Empty;
                btnShowReport.Enabled = false;
            }
        }

        private void btnShowReport_Click(object sender, EventArgs e)
        {
            var inventoryId = Business.PopulateInventory(_selectedStoreId, dtpStoreEndDate.Value);

            var prefetch = new PrefetchPath2(EntityType.InventoryItemEntity);
            prefetch.Add(InventoryItemEntity.PrefetchPathItem).SubPath.Add(ItemEntity.PrefetchPathUnit_);
            prefetch.Add(InventoryItemEntity.PrefetchPathShelf);

            _inventoryItems = new EntityCollection<InventoryItemEntity>();
            DALHelper.FetchCollection(_inventoryItems, new RelationPredicateBucket
                (InventoryItemFields.InventoryId == inventoryId), prefetch);

            if (_inventoryItems.Count <= 0)
            {
                MessageBox.Show(@"لا توجد أصناف بالمخزن");
            }
            ReportBindingSource.DataSource = _inventoryItems.Select
                (x => new InventoryReportTemplate
                          {
                              ItemCode = x.Item.Code,
                              ItemName = x.Item.Name,
                              ItemUnit = x.Item.Unit,
                              ActualBalance = x.QuantityCounted,
                              UnitPrice = Business.GetItemPrice(x.ItemId, _selectedStoreId),
                              TotalValue = Business.GetItemPrice(x.ItemId, _selectedStoreId) * x.QuantityCounted,
                              SystemBalance = x.Quantitystored,
                              Increase = x.QuantityCounted > x.Quantitystored
                                             ? x.QuantityCounted - x.Quantitystored
                                             : 0,
                              Decrease = x.QuantityCounted < x.Quantitystored
                                             ? x.Quantitystored - x.QuantityCounted
                                             : 0,

                              ValueOfIncreaseOrDecrease = (x.Quantitystored - x.QuantityCounted) > 0
                                                              ? (x.Quantitystored - x.QuantityCounted)*
                                                                Business.GetItemPrice(x.ItemId, _selectedStoreId)
                                                              : (x.QuantityCounted - x.Quantitystored)*
                                                                Business.GetItemPrice(x.ItemId, _selectedStoreId)
                          });
            this.reportViewer1.LocalReport.DataSources.Clear();
            this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ReportBindingSource));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("StoreName", lblStoreName.Text));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("StoreCode", txtStoreCode.Text));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("StoreEndDate", dtpStoreEndDate.Value.ToString()));

            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("HideQty", _hideQty.ToString()));

            var inventoryMan = Business.GetOneKeeperForStore(_selectedStoreId);
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("InventoryMan", inventoryMan));
            var manager = Business.GetStoreProjectManager(_selectedStoreId);
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ProjectManager", manager));

            this.reportViewer1.RefreshReport();
        }

        private void btnSearchStore_Click(object sender, EventArgs e)
        {
            var searchForm = new fromGeneralEntityPicker("Store");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtStoreCode.Text = searchForm.SelectedItemCode;
                _selectedStoreId = searchForm.SelectedItemId;
            }
        }
    }

    public class InventoryReportTemplate
    {
        public string ItemName { get; set; }
        public string ItemCode { get; set; }
        public string ItemUnit { get; set; }
        public decimal SystemBalance { get; set; }
        public decimal ActualBalance { get; set; }
        public decimal TotalValue { get; set; }
        public decimal Increase { get; set; }
        public decimal Decrease { get; set; }
        public decimal UnitPrice { get; set; }
        public decimal ValueOfIncreaseOrDecrease { get; set; }
    }
}
